package com.sttk.secreconeclick.bookkeep.dialog;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.lzb.basic.dialog.BaseSmartDialog;
import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.bean.BillBean;
import com.sttk.secreconeclick.bookkeep.bean.PlanBean;
import com.sttk.secreconeclick.bookkeep.databinding.DialogBillDetailBinding;
import com.sttk.secreconeclick.bookkeep.databinding.DialogPlanBinding;
import com.sttk.secreconeclick.bookkeep.utils.AppSpUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class PlanDialog extends BaseSmartDialog<DialogPlanBinding> implements View.OnClickListener {

    private int selPosition;
    private PlanBean planBean;

    public PlanDialog(@NonNull Context context) {
        super(context);
        this.selPosition = -1; // 默认为添加模式
    }

    public void setSelPosition(int selPosition) {
        this.selPosition = selPosition;
    }

    public void setPlanBean(PlanBean planBean) {
        this.planBean = planBean;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_plan;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvAdd.setOnClickListener(this);
        mDataBinding.tvStartDate.setOnClickListener(this);
        mDataBinding.tvEndDate.setOnClickListener(this);

        if (selPosition == -1) {
            // 添加模式
            planBean = new PlanBean();
            mDataBinding.tvAdd.setText(getContext().getString(R.string.add));
            String date = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd"));
            mDataBinding.tvStartDate.setText(date);
            mDataBinding.tvEndDate.setText(date);
            mDataBinding.etDuration.setText("0");
        } else {
            planBean = AppSpUtils.getPlanList().get(selPosition);
            // 修改模式
            mDataBinding.tvAdd.setText(getContext().getString(R.string.change));
            String startDate = TimeUtils.millis2String(planBean.getStartDate(), new SimpleDateFormat("yyyy-MM-dd"));
            String endDate = TimeUtils.millis2String(planBean.getEndDate(), new SimpleDateFormat("yyyy-MM-dd"));
            mDataBinding.etName.setText(planBean.getName());
            mDataBinding.etBudget.setText((int) planBean.getBudget() + "");
            mDataBinding.tvStartDate.setText(startDate);
            mDataBinding.tvEndDate.setText(endDate);
            mDataBinding.etDuration.setText((planBean.getEndDate() - planBean.getStartDate()) / (1000 * 60 * 60 * 24) + "");
        }

        mDataBinding.etDuration.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String str = charSequence.toString();
                if (TextUtils.isEmpty(str)) {
                    mDataBinding.tvEndDate.setText(mDataBinding.tvStartDate.getText().toString());
                } else {
                    int day = Integer.parseInt(str);
                    long startDate = TimeUtils.string2Millis(mDataBinding.tvStartDate.getText().toString(), new SimpleDateFormat("yyyy-MM-dd"));
                    long endDate = startDate + (day * (1000 * 60 * 60 * 24));
                    mDataBinding.tvEndDate.setText(TimeUtils.millis2String(endDate, new SimpleDateFormat("yyyy-MM-dd")));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            dismiss();
        } else if (id == R.id.tvStartDate) {
            showSelDate(true);
        } else if (id == R.id.tvEndDate) {
            showSelDate(false);
        } else if (id == R.id.tvAdd) {
            String name = mDataBinding.etName.getText().toString();
            // 验证输入
            if (TextUtils.isEmpty(name)) {
                ToastUtils.showShort(getContext().getString(R.string.please_input_name));
                return;
            }

            String budget = mDataBinding.etBudget.getText().toString();
            // 验证输入
            if (TextUtils.isEmpty(budget)) {
                ToastUtils.showShort(getContext().getString(R.string.please_input_budget));
                return;
            }

            long startDate = TimeUtils.string2Millis(mDataBinding.tvStartDate.getText().toString(), new SimpleDateFormat("yyyy-MM-dd"));
            long endDate = TimeUtils.string2Millis(mDataBinding.tvEndDate.getText().toString(), new SimpleDateFormat("yyyy-MM-dd"));

            if (startDate > endDate) {
                ToastUtils.showShort(getContext().getString(R.string.end_must_greater_start));
                return;
            }
            // 设置计划数据
            planBean.setName(name);
            planBean.setBudget(Double.parseDouble(budget));
            planBean.setStartDate(startDate);
            planBean.setEndDate(endDate);

            List<PlanBean> listSp = AppSpUtils.getPlanList();
            if (listSp == null) listSp = new ArrayList<>();

            if (selPosition == -1) {
                planBean.setRecordBeanList(new ArrayList<>());
                listSp.add(planBean);
            } else {
                listSp.set(selPosition, planBean);
            }

            AppSpUtils.savePlanList(listSp);

            if (listener != null) {
                listener.clickConfirm();
            }
            dismiss();
        }
    }

    private void showSelDate(boolean isStart) {
        SelDateDialog dateDialog = new SelDateDialog(getContext());
        if (isStart) {
            dateDialog.setDate(mDataBinding.tvStartDate.getText().toString());
        } else {
            dateDialog.setDate(mDataBinding.tvEndDate.getText().toString());
        }
        dateDialog.setListener(new SelDateDialog.Ilistener() {
            @Override
            public void clickConfirm(long date) {
                String dateStr = TimeUtils.millis2String(date, new SimpleDateFormat("yyyy-MM-dd"));
                if (isStart) {
                    mDataBinding.tvStartDate.setText(dateStr);
                } else {
                    mDataBinding.tvEndDate.setText(dateStr);
                }
                long startDate = TimeUtils.string2Millis(mDataBinding.tvStartDate.getText().toString(), new SimpleDateFormat("yyyy-MM-dd"));
                long endDate = TimeUtils.string2Millis(mDataBinding.tvEndDate.getText().toString(), new SimpleDateFormat("yyyy-MM-dd"));
                long day = (endDate - startDate) / (1000 * 60 * 60 * 24);
                mDataBinding.etDuration.setText(day + "");
            }
        });
        dateDialog.show();
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {

        void clickConfirm();

    }
}
